package MyHttp;

import java.util.HashMap;
import java.util.Map;

public abstract class Servlet {
    public abstract String doRequest(String method, String requestUrl, String requestBody);

    // 解析GET参数（URL中的查询字符串）
    protected Map<String, String> parseGetParams(String requestUrl) {
        Map<String, String> params = new HashMap<>();
        int queryIndex = requestUrl.indexOf("?");
        if (queryIndex != -1) {
            String query = requestUrl.substring(queryIndex + 1);
            for (String param : query.split("&")) {
                String[] parts = param.split("=");
                if (parts.length == 2) {
                    params.put(parts[0], parts[1]);
                }
            }
        }
        return params;
    }

    // 解析POST参数（请求体，假设为urlencoded格式）
    protected Map<String, String> parsePostParams(String requestBody) {
        Map<String, String> params = new HashMap<>();
        if (requestBody != null && !requestBody.isEmpty()) {
            for (String param : requestBody.split("&")) {
                String[] parts = param.split("=");
                if (parts.length == 2) {
                    params.put(parts[0], parts[1]);
                }
            }
        }
        return params;
    }
}